package com.hx.base.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hx.common.Globals;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CheckCodeServlet extends HttpServlet {

    private static final long serialVersionUID = -444847877494921526L;
    protected static final Log log = LogFactory.getLog(CheckCodeServlet.class);
    protected static final String randString = "23456789abcdeghkmnpqsuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
	@Override
    public final void service(final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException,
            IOException {

        final int WIDTH = 110;
        final int HEIGHT = 26;
        final int CODE_LENGTH = 5;

        // 生成图像,设置宽
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        Font font = new Font(g2d.getFont().getFontName(), Font.BOLD, 24);
        // 设定背景色
        g2d.setColor(Color.decode("#f4f4f4"));
        g2d.fillRect(0, 0, WIDTH, HEIGHT);
        g2d.setFont(font);
        // 设置线的着色
        g2d.setColor(new Color(9900));
        // 设置干扰图，加上随机位置的点点和线
        int end = RandomUtils.nextInt(3) * 3 + 2;
        for (int i = 0; i < end; ++i) {
            int x = RandomUtils.nextInt(image.getWidth());
            int y = RandomUtils.nextInt(image.getHeight());
            int xl = RandomUtils.nextInt(26);
            int yl = RandomUtils.nextInt(26);
            g2d.drawLine(x, y, x + xl, y + yl);
        }
        // 设置字的色
        g2d.setColor(new Color(9900));
        String checkCode = RandomStringUtils.random(CODE_LENGTH, randString);
        for (int i = 0; i < CODE_LENGTH; i++) {
            double rotate = (RandomUtils.nextInt(70) - 60) / 180.0;
            int x = 5 + i * 20;
            int y = 22;
            g2d.rotate(rotate, x, y);
            g2d.drawString(String.valueOf(checkCode.charAt(i)), x, y);
            g2d.rotate(-rotate, x, y);
        }
        g2d.dispose();

        // 验证码存入Cookie
        Cookie cookie;
        String from = request.getParameter("from");
        if(from != null && Globals.APPLY_VIP.equals(from)){
            cookie = new Cookie(Globals.APPLY_VIP, checkCode.toLowerCase());
        }else{
            cookie = new Cookie(Globals.USER_LOGIN_RAND, checkCode.toLowerCase());
        }
        cookie.setPath("/");
        response.addCookie(cookie);
    	OutputStream out = null;
        try {
        	out = response.getOutputStream();
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            image.flush();
            out.flush();
        } catch (Exception ex) {
            log.error(ex);
        }finally{
        	if(null != out)
            out.close();
        }
    }
}
